﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pictureAnalyse
{
    public partial class Form_ColorGroupSplit : Form
    {
        PictureGroup picGroup;

        public Form_ColorGroupSplit()
        {
            InitializeComponent();
        }

        private void Form_DragEnter(object sender, DragEventArgs e)
        {
            FileDrag.dragEnter(e);
        }

        private void Form_DragDrop(object sender, DragEventArgs e)
        {
            String paths = FileDrag.dragDrop(e);
            String[] files = paths.Split(';');
            if (files == null || files.Length == 0) return;

            String picPath = files[0];                      // 获取第一个图像
            Bitmap image = PictureGroup.getBitmap(picPath); // 载入图像
            setImage(pictureBox_preView, image);            // 在界面上显示载入的图像
            setPicGroup(image);                             // 载入分组信息
        }

        private void button_group_Click(object sender, EventArgs e)
        {
            //Int32.MaxValue
            //MessageBox.Show((float.MaxValue > 256 * 256 *256).ToString());
            //return;

            Bitmap image = picGroup.RemoveOutGroupBitmap(); // 移除最外层背景色
            image = PicTool.GetMinRect(image);               // 获取最小图像区域
            setImage(pictureBox_preView, image);            // 在界面中显示
            setPicGroup(image);                             // 载入分组信息
        }

        /// <summary>
        /// 在pictureBox上显示图像image
        /// 图像尺寸小于pictureBox尺寸时，居中显示
        /// 长或宽大于pictureBox尺寸时，使用Zoom进行显示
        /// </summary>
        private void setImage(PictureBox box, Bitmap image)
        {
            if (image.Width <= box.Width && image.Height <= box.Height) 
                box.SizeMode = PictureBoxSizeMode.CenterImage;
            else box.SizeMode = PictureBoxSizeMode.Zoom;

            box.Image = image;
        }

        /// <summary>
        /// 统计image的像素分组信息
        /// </summary>
        private void setPicGroup(Bitmap image)
        {
            // 获取分组信息
            picGroup = new PictureGroup(image);           // 对图像像素信息进行分组

            listBox_group.Items.Clear();
            foreach (ValueGroup group in picGroup.gChoice)     // 添加分组信息
                listBox_group.Items.Add(group.ToString());
        }

        private void listBox_group_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 获取所有选中项信息
            List<int> indexs = new List<int>();
            foreach (int i in listBox_group.SelectedIndices) indexs.Add(i);

            // 执行处理逻辑显示指定的分组图像
            Bitmap image = picGroup.getPicGroup(indexs, false);
            setImage(pictureBox_preView, image);
        }

        private void 应用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPicGroup(pictureBox_preView.Image as Bitmap);// 载入分组信息
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String path = PicTool.SaveToFile(pictureBox_preView.Image);
            PicTool.MessageWithOpen("图像已保存，是否查看?", path);
        }

        private void 色值统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_ColorViewer viewer = new Form_ColorViewer();
            viewer.ShowColorInfo(pictureBox_preView.Image as Bitmap);
            viewer.Show();
        }

        //浓缩颜色值
        private void button_shrink_Click(object sender, EventArgs e)
        {
            Bitmap image = PicTool.shrinkPixels(picGroup.image);    // 对图像进行颜色值浓缩处理
            //image = PicTool.shrinkPixels(image, 2);                 // 以2个像素为半径取均值
            //image = PicTool.shrinkPixels(image);                    // 以2个像素为半径取均值
            setImage(pictureBox_preView, image);                    // 在界面中显示
            setPicGroup(image);                                     // 载入分组信息
        }

        // 按连续图块自动分解图像
        private void button_blockSplit_Click(object sender, EventArgs e)
        {
            List<Bitmap> list = PicTool.GetContinuousRect(pictureBox_preView.Image); // 自动拆分子图
            String path = "";
            foreach(Bitmap pic in list)
            {
                path = PicTool.SaveToFile(pic);
            }
            
            PicTool.MessageWithOpen("图像已保存，是否查看?", path);
        }

        private void button_CirculeNum_Click(object sender, EventArgs e)
        {
            int N = PicTool.GetCiculesNum(pictureBox_preView.Image);
            MessageBox.Show("闭环数目：" + N);
        }
    }
}
